/**
 * @product: PCDC-CMS
 * @copyright: FSAI-南征北战信息科技
 * @filename: com.fsai.cms.controller.cms.ContController.java
 *
 * @link: Http://www.fastsai.com
 * @jdkv: Jdk 1.8
 */
package com.fsai.cms.controller.cms;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fsai.cms.base.BaseResp;
import com.fsai.cms.config.CmsSiteConfig;
import com.fsai.cms.domain.BaseSiteCfg;
import com.fsai.cms.domain.CmsCate;
import com.fsai.cms.domain.CmsCont;
import com.fsai.cms.service.ICmsCateService;
import com.fsai.cms.service.ICmsContService;
import com.fsai.cms.service.ICmsPubService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 *
 * @project: pcdc-cms-web
 *
 * @author: JaffeHe
 * @email: JaffeHe@foxmail.com
 * @date: 2021-03-13 12:01
 *
 * @ChangeLog<br/>
 * 				Developer Date Comment <br/>
 */
@RestController
@RequestMapping("/cont")
@Api(tags = "文章管理")
public class ContController {

	@Autowired
	private ICmsContService cmsContService;
	@Autowired
	private ICmsCateService cmsCateService;
	@Autowired
	private ICmsPubService pubService;
	@Autowired
	private CmsSiteConfig siteCfg;

	@ApiIgnore
	@GetMapping("/detail/{contId}")
	public ModelAndView detail(@PathVariable("contId") String contId, Model model) {
		//1查询文章信息
		CmsCont cmsCont = cmsContService.selectCmsContById(Long.parseLong(contId));
		model.addAttribute("cmsCont", cmsCont);
		//2查询栏目信息
		CmsCate cmsCate = cmsCateService.selectCmsCateById(cmsCont.getCateId());
		model.addAttribute("cmsCate", cmsCate);

		return new ModelAndView("pcdc/cont", "biz", model);
	}

	@ApiIgnore
	@GetMapping("/detail/{orgCode}/{contId}")
	public ModelAndView detailSub(@PathVariable("orgCode") String orgCode, @PathVariable("contId") String contId,
			Model model) {
		model.addAttribute("orgCode", orgCode);
		//1查询文章信息
		CmsCont cmsCont = cmsContService.selectCmsContById(Long.parseLong(contId));
		model.addAttribute("cmsCont", cmsCont);
		//2查询栏目信息
		CmsCate cmsCate = cmsCateService.selectCmsCateById(cmsCont.getCateId());
		model.addAttribute("cmsCate", cmsCate);

		return new ModelAndView("pcdc/subcont", "biz", model);
	}

	@GetMapping("/svlog/{contId}")
	@ApiOperation("获取机构站点栏目多层树")
	@ApiImplicitParams({ @ApiImplicitParam(name = "contId", value = "文章id", required = true) })
	public BaseResp saveLog(@PathVariable("contId") String contId, HttpServletRequest req) {
		int i = pubService.saveContVisitLog(contId, getIpAddr(req));

		return BaseResp.success(i);
	}

	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("PRoxy-Client-IP");
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("X-Real-IP");
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}

		return ip;
	}

	//主站文章搜索
	@ApiIgnore
	@GetMapping("/search")
	public ModelAndView search(String qyparam, Model model) {
		model.addAttribute("qyparam", qyparam);
		String orgCode = pubService.getSycConfVal("biz.deforg.code");
		//1查询搜索文章信息
		List<CmsCont> cmsConts = cmsContService.searchConts(qyparam, orgCode);
		model.addAttribute("cmsConts", cmsConts);
		//2站点默认图片
		BaseSiteCfg siteInfo = pubService.selSiteInfo(siteCfg.getMainId());
		model.addAttribute("siteInfo", siteInfo);

		return new ModelAndView("pcdc/contsearch", "biz", model);
	}

	//子站文章搜索
	@ApiIgnore
	@GetMapping("/{orgCode}/search")
	public ModelAndView orgSearch(@PathVariable("orgCode") String orgCode, String qyparam, Model model) {
		model.addAttribute("qyparam", qyparam);
		model.addAttribute("orgCode", orgCode);
		//1查询搜索文章信息
		List<CmsCont> cmsConts = cmsContService.searchConts(qyparam, orgCode);
		model.addAttribute("cmsConts", cmsConts);
		//2站点默认图片
		BaseSiteCfg siteInfo = pubService.selSiteInfo(siteCfg.getMainId());
		model.addAttribute("siteInfo", siteInfo);

		return new ModelAndView("pcdc/subcontsearch", "biz", model);
	}

}
